package de.bazinga.rotw.texture;

import java.net.URL;
import java.util.Hashtable;

import com.jme.image.Texture;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;

import de.bazinga.rotw.game.Game;

public class TextureCache {
	private static TextureCache instance = new TextureCache();
	
	private Hashtable<String,TextureState> textures = new Hashtable<String,TextureState>();
	
	private TextureCache() {
		
	}
	
	public static TextureCache getInstance() {
		if(instance == null) instance = new TextureCache();
		return instance;
	}
	
	public TextureState getTexture(URL filename) {
		if(!textures.containsKey(filename.getPath())) {
			// load texture
			TextureState texState = Game.getGame().getDisplay().getRenderer().createTextureState();
			Texture texture = TextureManager.loadTexture(filename,
					Texture.MinificationFilter.BilinearNearestMipMap ,
					Texture.MagnificationFilter.Bilinear);
			
			texture.setWrap(Texture.WrapMode.Repeat);
			
			texState.setTexture(texture);
			texState.setEnabled(true);
			
			textures.put(filename.getPath(), texState);
		}
		
		return textures.get(filename.getPath());
	}
	
	public TextureState getTexture(String filename) {
		return getTexture(Game.class.getClassLoader().getResource(filename));
	}
}
